package cn.edu.fjnu.camera;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;

public class ImgUtil {
	
	public static Bitmap ResizeBitmap(Bitmap bitmap, int newWidth) {
		
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float temp = ((float) height) / ((float) width);
		int newHeight = (int) ((newWidth) * temp);
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		Matrix matrix = new Matrix();
		// resize the bit map
		matrix.postScale(scaleWidth, scaleHeight); 
		// matrix.postRotate(45);
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		return resizedBitmap;

	}
	
	public static Bitmap AbsResizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
		
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float temp = ((float) height) / ((float) width);
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		Matrix matrix = new Matrix();
		// resize the bit map
		matrix.postScale(scaleWidth, scaleHeight); 
		// matrix.postRotate(45);
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		return resizedBitmap;
	}

	/**将Bitmap转成文件放在程序文件目录下*/
	public static void Bitmap2File(Bitmap bitmap, String fileName, Context ctx) {
		
		try {			
			FileOutputStream fos = ctx.openFileOutput(fileName, android.content.Context.MODE_WORLD_READABLE);
			bitmap.compress(CompressFormat.JPEG, 80, fos);			
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
	}
	/**将Bitmap转成文件放在制定的文件目录下*/
	public static String Bitmap2File(Bitmap bitmap, String fileName, String path) {
//		U.dout(fileName+","+path);
		try {
			String [] str = fileName.split("\\.");
//			U.dout(str);
			String newfileName = str[0];
			
//			U.dout("newfileName="+newfileName);
			CompressFormat compressFormat;
			if(fileName.toLowerCase().endsWith(".png")){
				newfileName += ".png";
				compressFormat = CompressFormat.PNG;
			}else{
				newfileName += ".jpg";
				compressFormat = CompressFormat.JPEG;
			}
//			U.dout(newfileName+","+path);
			File pic = new File(path+newfileName);
			FileOutputStream fos = new FileOutputStream(pic);
			bitmap.compress(compressFormat, 100, fos);
			fos.flush();
			fos.close();
			return path+newfileName;
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
		return "error";
	}
	
	//将字节数组转换为Bitmap
	public static Bitmap Bytes2Bimap(byte[] b){   
        if(b.length!=0){   
            return BitmapFactory.decodeByteArray(b, 0, b.length);   
        }   
        else {   
            return null;   
        }
	}
	//将Bitmap转换为字节数组
	public static byte[] Bitmap2Bytes(Bitmap bm){   
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();     
	    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);     
	    return baos.toByteArray();   
	   }  
}
